# Django AdminLTE3 Simple Setup Script
# Simple version to avoid encoding issues

Write-Host "Starting Django AdminLTE3 project setup..." -ForegroundColor Green

# Check Python installation
try {
    $pythonVersion = python --version 2>&1
    Write-Host "Python version detected: $pythonVersion" -ForegroundColor Yellow
} catch {
    Write-Host "Error: Python not detected, please install Python 3.8+" -ForegroundColor Red
    exit 1
}

# Create virtual environment
Write-Host "Creating virtual environment..." -ForegroundColor Yellow
if (Test-Path ".venv") {
    Write-Host "Virtual environment already exists, skipping creation" -ForegroundColor Yellow
} else {
    python -m venv .venv
    Write-Host "Virtual environment created successfully" -ForegroundColor Green
}

# Activate virtual environment
Write-Host "Activating virtual environment..." -ForegroundColor Yellow
& ".venv\Scripts\Activate.ps1"

# Upgrade pip
Write-Host "Upgrading pip..." -ForegroundColor Yellow
python -m pip install --upgrade pip

# Create requirements.txt
Write-Host "Creating requirements.txt..." -ForegroundColor Yellow
$requirements = @"
Django>=4.2.0,<5.0.0
django-adminlte3>=0.1.6
matplotlib>=3.7.0
seaborn>=0.12.0
plotly>=5.15.0
bokeh>=3.2.0
pandas>=2.0.0
numpy>=1.24.0
django-debug-toolbar>=4.1.0
django-extensions>=3.2.0
whitenoise>=6.5.0
python-decouple>=3.8
"@

$requirements | Out-File -FilePath "requirements.txt" -Encoding UTF8
Write-Host "Requirements.txt created" -ForegroundColor Green

# Install dependencies
Write-Host "Installing dependencies..." -ForegroundColor Yellow
pip install -r requirements.txt

# Create Django project in current directory
Write-Host "Creating Django project..." -ForegroundColor Yellow
python -m django startproject demo .

# Create necessary directories
Write-Host "Creating project directories..." -ForegroundColor Yellow
New-Item -ItemType Directory -Path "static" -Force | Out-Null
New-Item -ItemType Directory -Path "static/css" -Force | Out-Null
New-Item -ItemType Directory -Path "static/js" -Force | Out-Null
New-Item -ItemType Directory -Path "static/img" -Force | Out-Null
New-Item -ItemType Directory -Path "templates" -Force | Out-Null
New-Item -ItemType Directory -Path "media" -Force | Out-Null
New-Item -ItemType Directory -Path "data" -Force | Out-Null

# Create Django app
Write-Host "Creating Django app..." -ForegroundColor Yellow
python manage.py startapp dashboard

# Create environment file
Write-Host "Creating environment configuration..." -ForegroundColor Yellow
$envContent = @"
DEBUG=True
SECRET_KEY=your-secret-key-here-change-in-production
ALLOWED_HOSTS=localhost,127.0.0.1
DATABASE_URL=sqlite:///db.sqlite3
STATIC_URL=/static/
STATIC_ROOT=staticfiles/
MEDIA_URL=/media/
MEDIA_ROOT=media/
ADMINLTE3_THEME=default
"@

$envContent | Out-File -FilePath ".env" -Encoding UTF8

# Configure Django project
Write-Host "Configuring Django project..." -ForegroundColor Yellow
python configure_django.py

Write-Host "Project setup completed successfully!" -ForegroundColor Green
Write-Host "Next steps:" -ForegroundColor Yellow
Write-Host "1. Run 'python manage.py migrate' to initialize database" -ForegroundColor White
Write-Host "2. Run 'python manage.py runserver' to start development server" -ForegroundColor White
Write-Host "3. Visit http://127.0.0.1:8000 to view the project" -ForegroundColor White
Write-Host "4. Edit .env file to configure environment variables" -ForegroundColor White
